import { _decorator, Component, Node, tween, Vec3, Vec2, director, view, UITransform, View, Label } from 'cc';
import { BundlesConst } from '../../ccc3-framework-core/const/Const';
import { audioManager } from '../../ccc3-framework-core/framework/modules/audioManager';
import dataUtil from '../../ccc3-framework-core/framework/modules/dataUtil';
import { entityUtil } from '../../ccc3-framework-core/framework/modules/entityUtil';
import { uiManager } from '../../ccc3-framework-core/framework/modules/uiManager';
import { gameState } from '../../ccc3-framework-core/gameState';
import { dropMoneyModel } from '../../ccc3-framework-core/model/dropMoneyModel';
import Common from '../../ccc3-framework-core/utils/Common';
import { BundlesDefine, entityDefine } from '../CustomDefine';
const { ccclass, property } = _decorator;

@ccclass('moneyUtil')
export class moneyUtil {

    private static _instance: moneyUtil;
    static get instance() {
        if (this._instance) {
            return this._instance;
        }

        this._instance = new moneyUtil();
        return this._instance;
    }

    private get coinBarPos(): Vec3 {
        let width = View.instance.getVisibleSize().width;
        let height = View.instance.getVisibleSize().height;
        let x = width / 2 - 50 - 113.271 - 209.057 / 2;
        let y = height / 2 - 54.804;
        return new Vec3(x, y, 0);
    };
    private speed: number = 2200;
    public createCoinAnim(uiPos: Vec3, callback: () => void, uiRoot: Node = null) {
        if (!uiRoot) {
            uiRoot = uiManager.uiRoot;
        }
        entityUtil.showEntity(entityDefine.money2d, uiRoot, (money2d) => {
            money2d.setSiblingIndex(uiRoot.children.length - 1);
            //旋转随机角度
            let angle = Common.randomNumBoth(0, 360);
            let dir = new Vec2(0, 1).rotate(angle);
            //向量放大
            let pos = dir.multiplyScalar(80);

            let temp = new Vec3(0, 0, 0);
            let p3 = Vec3.add(temp, uiPos, new Vec3(pos.x, pos.y, 0));
            let p4 = p3.add(new Vec3(pos.x, pos.y, 0))
            money2d.position = uiPos;// Vec3.add(temp2, parent.position, temp);
            money2d.scale = new Vec3(0.5, 0.5, 0.5);
            let duration1 = Vec3.distance(uiPos, p3) / 1000;
            tween(money2d).to(duration1, {
                position: p3,
                scale: new Vec3(1.5, 1.5, 1.5),
            }, { easing: "sineIn" })
                .delay(0.3)
                .call(() => {
                    let duration2 = Vec3.distance(money2d.position, this.coinBarPos) / 1500;
                    tween(money2d).to(duration2, {
                        position: this.coinBarPos,
                        scale: new Vec3(1, 1, 1),
                    }, { easing: "sineOut" })
                        .call(() => {
                            audioManager.playAudio("coin");
                            entityUtil.hideEntity(money2d);
                            callback();
                        })
                        .start();

                }).start();
        }, BundlesConst.main);
    }

    public flyCoin(uiPos: Vec3, callback: () => void, playNum: number = 10, uiRoot: Node = null) {
        let endNum = 0;
        for (let i = 0; i < playNum; i++) {
            this.createCoinAnim(uiPos, () => {
                endNum++;
                if (endNum >= playNum) {
                    if (callback)
                        callback();
                }
            }, uiRoot);
        }
    }

    public flyNum(num: number, uiPos: Vec3, callback?: () => void, uiRoot: Node = null) {
        if (!uiRoot) {
            uiRoot = uiManager.uiRoot;
        }
        entityUtil.showEntity(entityDefine.animCoinNum, uiRoot, (numNode) => {
            numNode.position = uiPos;
            numNode.parent = uiRoot;
            numNode.children[0].getComponent(Label).string = `+${num}`;
            numNode.setSiblingIndex(uiRoot.children.length - 1);
            tween(numNode).by(1, {
                position: new Vec3(0, 100, 0)
            }, { easing: "sineIn" })
                .call(() => {
                    entityUtil.hideEntity(numNode);
                    if (callback)
                        callback();
                }).start();
        }, BundlesDefine.town);
    }



    public setDropMoney() {
        let moneyList = entityUtil.getAllEntity(entityDefine.money3d);
        let dropMoneyArray: Array<dropMoneyModel> = [];
        let dropPusaArray: Array<dropMoneyModel> = [];
        if (moneyList) {
            moneyList.forEach(item => {
                if (item instanceof Node) {
                    let pc: any = item.getComponent("paymentCoin");
                    if (pc.moneyType == 1) {
                        dropMoneyArray.push({
                            position: item.position,
                            amount: pc.coin
                        })
                    }
                    else {
                        dropPusaArray.push({
                            position: item.position,
                            amount: pc.coin
                        })
                    }
                }
                else {
                    debugger
                }

            });
            dataUtil.setDropMoney(dropMoneyArray);
            dataUtil.setPusaMoney(dropPusaArray);
        }

    }
}

